<?php

class M_payment extends CI_Model
{

    
    function insert_invoice($accountid, $type, $start_date, $total, $invoice_status, $payment_description)
    {
        $data['PAYMENT_DESCRIPTION'] = $payment_description;
    	$data['ACCOUNT_ID']=$accountid;
        $data['INVOICE_TYPE']=$type;
        $data['START_DATE']=$start_date;
        $data['TOTAL']=$total;
        $data['INVOICE_STATUS']=$invoice_status;
        return $this->db->insert('INVOICE',$data);
    }
    
    function insert_invoice_detail($invoice_id, $item_id, $quantity)
    {
    	$data['INVOICE_ID'] = $invoice_id;
    	$data['ITEM_ID'] = $item_id;
    	$data['QUANTITY'] = $quantity;
    	return $this->db->insert('invoice_detail', $data);
    }

    function load_list_invoice_by_accountid($id)
    {
    	$this->db->where('ACCOUNT_ID', $id);
    	$this->db->where('INVOICE_STATUS',2);
    	$result = $this->db->get('invoice');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }

    function load_invoice_detail_by_invoiceid($id)
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE, invoice_detail.*
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            inner join invoice_detail on item.item_id = invoice_detail.item_id where invoice_detail.invoice_id = $id
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;
    	
        /*$this->db->where('INVOICE_ID', $id);
    	$this->db->join('item','item.ITEM_ID=invoice_detail.ITEM_ID');
    	$result = $this->db->get('invoice_detail');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }
    
}


?>